#!/usr/bin/env node

/**
 * This script helps set up Bun for the protoactor-ts project
 */

const { execSync } = require('child_process');
const { existsSync } = require('fs');
const { resolve } = require('path');

console.log('🚀 Setting up Bun for protoactor-ts...');

// Check if Bun is installed
try {
  execSync('bun --version', { stdio: 'ignore' });
  console.log('✅ Bun is already installed');
} catch (error) {
  console.log('❌ Bun is not installed. Installing Bun...');
  
  try {
    // Try to install Bun with npm
    execSync('npm install -g bun', { stdio: 'inherit' });
    console.log('✅ Bun installed successfully');
  } catch (npmError) {
    console.error('❌ Failed to install Bun with npm. Please install manually:');
    console.log('   curl -fsSL https://bun.sh/install | bash');
    process.exit(1);
  }
}

// Install dependencies with Bun
console.log('📦 Installing dependencies with Bun...');
try {
  execSync('bun install', { stdio: 'inherit' });
  console.log('✅ Dependencies installed successfully');
} catch (error) {
  console.error('❌ Failed to install dependencies:', error);
  process.exit(1);
}

// Try to run a test build
console.log('🏗️ Testing build with Bun...');
try {
  execSync('bun run build', { stdio: 'inherit' });
  console.log('✅ Build successful');
} catch (error) {
  console.error('❌ Build failed. Please check the errors above.');
  process.exit(1);
}

// Success message
console.log('\n🎉 Bun setup complete! You can now use the following commands:');
console.log('   - bun run build     - Build the project');
console.log('   - bun run test      - Run tests');
console.log('   - bun run examples  - Run examples');
console.log('\n📖 See BUN_SETUP.md for more information on using Bun with this project.'); 